local fushi = fk.CreateSkill{
  name = "rfenghou_3d9j__fushi",
}

local U = require "packages.utility.utility"

local function fushiMove(player, target, name)
  local hands = target:getCardIds("h")
  if #hands == 0 then return end
  local room = player.room
  local mark = player:getMark("@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang")
  local names = mark.list_names
  local all_cards = mark.list_cards
  local index = table.indexOf(names, name)
  local ctable = table.simpleClone(all_cards[index])
  room:moveCards(
    {
      ids = ctable,
      from = player,
      toArea = Card.Processing,
      moveReason = fk.ReasonExchange,
      proposer = player,
      skillName = fushi.name,
      moveVisible = true
    },
    {
      ids = hands,
      from = target,
      toArea = Card.Processing,
      moveReason = fk.ReasonExchange,
      proposer = player,
      skillName = fushi.name,
      moveVisible = true
    }
  )
  room:moveCards(
    {
      ids = hands,
      to = player,
      toArea = Card.PlayerSpecial,
      specialName = "#rfenghou_3d9j__godzhenji_shuixiang",
      moveReason = fk.ReasonExchange,
      proposer = player,
      skillName = fushi.name,
      moveVisible = true
    },
    {
      ids = ctable,
      to = target,
      toArea = Card.PlayerHand,
      moveReason = fk.ReasonExchange,
      proposer = player,
      skillName = fushi.name,
      moveVisible = true
    }
  )
  room:cleanProcessingArea(table.connect(hands, ctable), fushi.name)
  all_cards[index] = hands
  room:setPlayerMark(player, "@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang", mark)
end

local spec = { ---@type TrigSkelSpec<fun(self: TriggerSkill, event: UseCardEvent | RespondCardEvent, target: ServerPlayer, player: ServerPlayer, data: UseCardData | RespondCardData):any>
  mute = true,
  max_turn_use_time = 1,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fushi.name) and self:withinTimesLimit(player, Player.HistoryTurn) and data.card and data.card.type == Card.TypeBasic and not player:isKongcheng() and
      player:getMark("@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang") ~= 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang")
    local names = mark.list_names
    local all_cards = mark.list_cards
    local choice = U.askForChooseCardList(room, player, names, all_cards, 1, 1, fushi.name, "#rfenghou_3d9j__fushi-invoke", false, true)
    if #choice ~= 0 then
      event:setCostData(self, choice)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    fushiMove(player, player, event:getCostData(self)[1])
    local males = table.filter(room:getAlivePlayers(false), function(p)
      return p:isMale() and not p:isKongcheng()
    end)
    if #males == 0 then return end
    local to = room:askToChoosePlayers(player, {
      targets = males,
      min_num = 1,
      max_num = 1,
      skill_name = fushi.name,
      prompt = "#rfenghou_3d9j__fushi-switch",
    })[1]
    if to == nil then return end
    local mark = player:getMark("@[rfenghou__cardlist]rfenghou_3d9j__godzhenji_shuixiang")
    local names = mark.list_names
    local all_cards = mark.list_cards
    local choice = U.askForChooseCardList(room, to, names, all_cards, 1, 1, fushi.name, "#rfenghou_3d9j__fushi-invoke", false, true)
    if #choice ~= 0 then
      fushiMove(player, to, choice[1])
    end
  end,
}

fushi:addEffect(fk.CardUseFinished, spec)
fushi:addEffect(fk.CardRespondFinished, spec)

Fk:loadTranslationTable{
  ["rfenghou_3d9j__fushi"] = "浮世",
  [":rfenghou_3d9j__fushi"] = "每名角色的回合限一次，当你使用或打出的基本牌结算后，你可以用所有手牌交换一份“水相”，然后你可以指定一名男性角色，其可以用所有手牌交换其指定的一份“水相”。",

  ["#rfenghou_3d9j__fushi-invoke"] = "浮世：你可以用所有手牌交换一份“水相”",
  ["#rfenghou_3d9j__fushi-switch"] = "浮世：你可以令一名男性角色用所有手牌交换一份“水相”",
}

return fushi
